package com.lishe.lishe.asynchttp;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;

/**
 * Created by lwa on 2017/8/11.
 */

public class HttpUtils {

    private static final String TAG = HttpUtils.class.getSimpleName();

    //---------------------------------AsyncHttp-封装-------------------------------------------
    private static AsyncHttpClient client = new AsyncHttpClient();


    /**
     * get网络请求
     *
     * @param url
     * @param params
     * @param responseHandlerInterface
     */
    public static void get(String url, RequestParams params, ResponseHandlerInterface responseHandlerInterface) {
        Log.e(TAG,"get:"+url);
        setting();
        client.get(url, params, responseHandlerInterface);
    }

    /**
     * post网络请求
     *
     * @param url
     * @param params
     * @param responseHandlerInterface
     */
    public static void post(String url, RequestParams params, ResponseHandlerInterface responseHandlerInterface) {
        Log.e(TAG,"post:"+url);
        setting();
        client.post(url, params, responseHandlerInterface);
    }


    /**
     * post网络请求
     * @param context 传递Context才能取消上个请求
     * @param url
     * @param params
     * @param responseHandlerInterface
     */
    public static void post(Context context, String url, RequestParams params, ResponseHandlerInterface responseHandlerInterface) {
        Log.e(TAG,"post:"+url);
        setting();
        client.post(context,url, params, responseHandlerInterface);
    }


    public static String getAbsoluteUrl(String relativeUrl) {
        return relativeUrl;
    }

    public static void setting() {
        client.setTimeout(20000);//20秒
        client.setURLEncodingEnabled(true);
    }

    public static void cancelAllRequest(){
        client.cancelAllRequests(true);
    }



}
